using FireStarterModeller.BO;
using FireStarterModeller.UI;
using Habanero.Base;
using Habanero.BO;
using Habanero.Test.UI.Base;
using Habanero.UI.Base;
using NUnit.Framework;

namespace FireStarterModeller.Test.UI
{
    /// <summary>
    /// This control implements the IBOSelector Interface and should therefore fulfill the interface
    /// </summary>
    [TestFixture]
    public class TestDMAssemblyCtrl_AsBOSelector : TestBOSelectorEditableGridWin
    {
        [TestFixtureSetUp]
        public void SetupFixture()
        {
            UITestUtils.SetupFixture();
        }

        [SetUp]
        public virtual void SetupTest()
        {
            UITestUtils.SetupTest();
        }

/*        protected override void SetSelectedIndex(IBOColSelectorControl colSelector, int index)
        {
            int count = 0;

            IEditableGrid gridControl = ((IEditableGridControl)((DMClassSelectorControl)colSelector).GridControl).Grid;
            foreach (IDataGridViewRow row in gridControl.Rows)
            {
                if (row == null) continue;//This is done to stop the Pragma warning.
                if (count == index)
                {
                    IBusinessObject businessObjectAtRow = gridControl.GetBusinessObjectAtRow(count);
                    colSelector.SelectedBusinessObject = businessObjectAtRow;
                }
                count++;
            }
        }*/

/*        protected override int SelectedIndex(IBOColSelectorControl colSelector)
        {
            IEditableGrid gridControl = ((IEditableGridControl)((DMClassSelectorControl)colSelector).GridControl).Grid;
            IDataGridViewRow currentRow = null;
            if (gridControl.SelectedRows.Count > 0)
            {
                currentRow = gridControl.SelectedRows[0];
            }

            if (currentRow == null) return -1;

            return gridControl.Rows.IndexOf(currentRow);
        }*/

        protected override IBusinessObject CreateNewBO()
        {
            return new DMClass { ClassNameBO = "B" };
        }
        protected static DMClass CreateNewBDClass(string className)
        {
            return new DMClass { ClassNameBO = className };
        }
/*        protected override IBOColSelectorControl CreateSelector()
        {
            DMClassSelectorControl dmSolutionControl = new DMClassSelectorControl(GetControlFactory());
            System.Windows.Forms.Form frm = new System.Windows.Forms.Form();
            frm.Controls.Add(dmSolutionControl);
            return dmSolutionControl;
        }*/
        protected override IBusinessObjectCollection GetCollectionWith_4_Objects()
        {
            DMClass cp = CreateNewBDClass("b");
            DMClass cp2 = CreateNewBDClass("d");
            DMClass cp3 = CreateNewBDClass("c");
            DMClass cp4 = CreateNewBDClass("a");
            return new BusinessObjectCollection<DMClass> { cp, cp2, cp3, cp4 };
        }
        protected override IBusinessObjectCollection GetCollectionWithNoItems()
        {
            return new BusinessObjectCollection<DMClass>();
        }

        protected override IBusinessObjectCollection GetCollectionWithOneBO(out IBusinessObject bo)
        {
            bo = new DMClass();
            return new BusinessObjectCollection<DMClass> { (DMClass)bo };
        }
        protected override IBusinessObjectCollection GetCollectionWithTowBOs(out IBusinessObject bo)
        {
            bo = new DMClass();
            DMClass myBO2 = new DMClass();
            return new BusinessObjectCollection<DMClass> { (DMClass)bo, myBO2 };
        }

        [Test]
        public override void Test_Constructor_ReadOnlyGridControlSet()
        {
            //---------------Set up test pack-------------------

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IBOColSelectorControl colSelector = CreateSelector();
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(DMClassSelectorControl), colSelector);
        }
        [Test]
        public override void Test_SetBOCollection_WhenAutoSelectsFirstItem_ShouldSelectFirstItem()
        {
            //---------------Set up test pack-------------------
            IBOColSelectorControl colSelector = CreateSelector();
            IBusinessObject myBO;
            IBusinessObjectCollection collection = GetCollectionWithTowBOs(out myBO);
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, colSelector.NoOfItems, "editable grid has a new row");
            Assert.AreEqual(0, SelectedIndex(colSelector), "Editable grid new row is selected");
            Assert.AreEqual(null, colSelector.SelectedBusinessObject);
            //---------------Execute Test ----------------------
            colSelector.BusinessObjectCollection = collection;
            //---------------Test Result -----------------------
            Assert.AreEqual(collection.Count + NumberOfLeadingBlankRows(), colSelector.NoOfItems, "The blank item");
            Assert.AreSame(myBO, colSelector.SelectedBusinessObject);
            Assert.AreEqual(ActualIndex(0), SelectedIndex(colSelector));
        }

        public override void Test_AutoSelectsFirstItem_NoItems()
        {
            //---------------Set up test pack-------------------
            IBOColSelectorControl colSelector = CreateSelector();
            IBusinessObjectCollection collection = GetCollectionWithNoItems();
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, colSelector.NoOfItems, "editable grid has a new row");
            Assert.AreEqual(0, SelectedIndex(colSelector), "Editable grid new row is selected");
            Assert.AreEqual(null, colSelector.SelectedBusinessObject);
            //---------------Execute Test ----------------------
            colSelector.BusinessObjectCollection = collection;
            //---------------Test Result -----------------------
            Assert.AreEqual(NumberOfLeadingBlankRows(), colSelector.NoOfItems, "The blank item");
            Assert.AreSame(null, colSelector.SelectedBusinessObject);
            Assert.AreEqual(0, SelectedIndex(colSelector));
        }
        [Test]
        public override void Test_SetBOCollection_WhenAutoSelectFalse_ShouldNot_AutoSelectsFirstItem()
        {
            //---------------Set up test pack-------------------
            IBOColSelectorControl colSelector = CreateSelector();
            IBusinessObject myBO;
            IBusinessObjectCollection collection = GetCollectionWithTowBOs(out myBO);
            colSelector.AutoSelectFirstItem = false;
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, colSelector.NoOfItems);
            Assert.AreEqual(0, SelectedIndex(colSelector));
            Assert.AreEqual(null, colSelector.SelectedBusinessObject);
            Assert.IsFalse(colSelector.AutoSelectFirstItem);
            //---------------Execute Test ----------------------
            colSelector.BusinessObjectCollection = collection;
            //---------------Test Result -----------------------
            Assert.AreEqual(ActualNumberOfRows(collection.Count), colSelector.NoOfItems, "The blank item");
            Assert.IsNull(colSelector.SelectedBusinessObject);
        }
    }
}